package com.demo.servlet.demo1221.demo01;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

//@WebListener
public class MyListener2 implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        System.out.println("往 ServletContext 添加属性：" + event.getName() + " = " + event.getValue());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        System.out.println("往 ServletContext 移除属性：" + event.getName() + " = " + event.getValue());
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        //event 对象获取到被替换前的 Value
        System.out.println("往 ServletContext 替换属性：" + event.getName() + " = " + event.getValue());
        //获取被替换后的 Value
        System.out.println("ServletContext 替换后的属性：" + event.getName() + " = " + event.getServletContext().getAttribute(event.getName()));
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        System.out.println("往 ServletRequest 添加属性：" + srae.getName() + " = " + srae.getValue());
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        System.out.println("往 ServletRequest 移除属性：" + srae.getName() + " = " + srae.getValue());
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        System.out.println("往 ServletRequest 替换属性：" + srae.getName() + " = " + srae.getValue());
        //获取被替换后的 Value
        System.out.println("ServletRequest 替换后的属性：" + srae.getName() + " = " + srae.getServletRequest().getAttribute(srae.getName()));
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("往 HttpSession 添加属性：" + event.getName() + " = " + event.getValue());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        System.out.println("往 HttpSession 移除属性：" + event.getName() + " = " + event.getValue());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        System.out.println("往 HttpSession 替换属性：" + event.getName() + " = " + event.getValue());
        //获取被替换后的 Value
        System.out.println("HttpSession 替换后的属性：" + event.getName() + " = " + event.getSession().getAttribute(event.getName()));
    }
}
